package com.ruiaa.meeting.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.ruiaa.meeting.common.CommonResponse;
import com.ruiaa.meeting.common.Constant;
import com.ruiaa.meeting.common.ResponseCode;
import com.ruiaa.meeting.model.User;
import com.ruiaa.meeting.model.vo.UserVo;
import com.ruiaa.meeting.service.AdminService;
import com.ruiaa.meeting.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
//import org.hibernate.validator.constraints.NotBlank;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author chenhuaiyi
 * @date 2022-11-04 11:16
 */

/**
 * @author kiyoshi
 */
@RestController
@RequestMapping("/account")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public CommonResponse<UserVo> login(
            @RequestParam @NotBlank(message = "用户名不能为空") String username,
            @RequestParam @NotBlank(message = "密码不能为空") String password){
        return userService.login(username, password);
    }

    @PostMapping("/isLogin")
    // 加上SaCheckRole,那个级别的用户可以访问就加上哪个级别，注意是向下兼容
    // ADMIN可以访问ADMIN和USER的接口，不能访问SUPER_ADMIN的接口
    @SaCheckRole(Constant.Role.SUPER_ADMIN)
    public CommonResponse<Object> isLogin(){
        return CommonResponse.createForSuccess("已经登录");
    }

    @GetMapping("/admins")
    @SaCheckRole(Constant.Role.SUPER_ADMIN)
    public CommonResponse<List<User>> getAllAdmin(){
        String username=(String) StpUtil.getLoginId();
        User user=(User) StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return userService.getAllAdmin();

    }

    @PostMapping("/admin")
    @SaCheckRole(Constant.Role.SUPER_ADMIN)
    public CommonResponse<Object> addAdmin(
            @RequestParam @NotBlank(message = "用户名不能为空") String username,
            @RequestParam @NotBlank(message = "密码不能为空") String password
    ){
        String username1=(String) StpUtil.getLoginId();
        User user=(User) StpUtil.getSession().get(username1);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return userService.addAdmin(username,password);

    }

    @DeleteMapping("/admin")
    @SaCheckRole(Constant.Role.SUPER_ADMIN)
    public CommonResponse<Object> removeAdmin(
            @RequestParam @NotBlank(message = "用户名不能为空") String username
    ){
        String username1=(String) StpUtil.getLoginId();
        User user=(User) StpUtil.getSession().get(username1);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return userService.removeAdmin(username);
    }

    @PutMapping("/admin")
    @SaCheckRole(Constant.Role.SUPER_ADMIN)
    public CommonResponse<UserVo> updateAdmin(
            @RequestBody @Valid User user
    ){
        String username1=(String) StpUtil.getLoginId();
        User user1=(User) StpUtil.getSession().get(username1);
        if(user1 == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return userService.updateAdmin(user);
    }

    @PostMapping("/admin/set_password")
    @SaCheckRole(Constant.Role.SUPER_ADMIN)
    public CommonResponse<UserVo> setAdminPassword(
            @RequestParam @NotBlank(message = "用户名不能为空") String username,
            @RequestParam @NotBlank(message = "修改密码不能为空") String password
    ){
        String username1=(String) StpUtil.getLoginId();
        User user1=(User) StpUtil.getSession().get(username1);
        if(user1 == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return userService. setAdminPassword(username,password);
    }

    @PostMapping("/admin/disable")
    @SaCheckRole(Constant.Role.SUPER_ADMIN)
    public CommonResponse<Object> disableAdmin(
            @RequestParam @NotBlank(message = "用户名不能为空") String username
    ){
        String username1=(String) StpUtil.getLoginId();
        User user=(User) StpUtil.getSession().get(username1);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return userService.disableAdmin(username);
    }

    @PostMapping("/admin/able")
    @SaCheckRole(Constant.Role.SUPER_ADMIN)
    public CommonResponse<Object> ableAdmin(
            @RequestParam @NotBlank(message = "用户名不能为空") String username
    ){
        String username1=(String) StpUtil.getLoginId();
        User user=(User) StpUtil.getSession().get(username1);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return userService.ableAdmin(username);
    }

//普通管理员
//    @GetMapping("/users")
//    @SaCheckRole(Constant.Role.ADMIN)
//    public CommonResponse<List<UserVo>> getAllUser(){
//        return null;
//    }

    @PostMapping("/user")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<User> addUser(
            @RequestParam @NotBlank(message = "用户名不能为空") String username,
            @RequestParam @NotBlank(message = "密码不能为空") String password
    ){
           return adminService.addUser(username,password);
    }

    @DeleteMapping("/user")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<Object> deleteUser(
            @RequestParam @NotBlank(message = "用户名不能为空") String username
    ){
        return adminService.deleteUser(username);
    }

    @PutMapping("/user")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<Object> updateUser(
            @RequestBody @Valid User user
    ){
       return adminService.updateUser(user);
    }

    @GetMapping("/user")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<Object> selectUser(
            @RequestParam @NotBlank(message = "用户名不能为空")String usernameList){
        return adminService.selectUser(usernameList);
    }

    @PostMapping("/user/set_password")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<Object> modifyPassword(
            @RequestParam @NotBlank(message = "用户名不能为空") String username,
            @RequestParam @NotBlank(message = "密码不能为空") String password
    ){
        return adminService.modifyUserPassword(username,password);
    }

    @PostMapping("/user/disable")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<Object> banUser(
            @RequestParam @NotBlank(message = "用户名不能为空") String username
    ){
        return adminService.banUser(username);
    }

    @PostMapping("/user/able")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<Object> defrostUser(
            @RequestParam @NotBlank(message = "用户名不能为空") String username
    ){
        return adminService.defrostUser(username);
    }
}
